import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
  token: getToken(),
  userInfo: {
    id: '',
    name: '',
    sex: '',
    passwrod: '',
    account: '',
    img: '',
    role: 0,
    state: '',
    email: '',
    phone: '',
    createTime: '',
    updateTime: ''
  },
  account: '',
  name: '',
  // avatar: '',
  // introduction: '',
  role: [],
  // 新增数据：
  id: '',
  email: '',
  phone: '',
  createTime: '',
  updateTime: '',
  img: '',
  sex: '',
  states: ''
}
// role.push({
//
// })

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  // SET_INTRODUCTION: (state, introduction) => {
  //   state.introduction = introduction
  // },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo.id = userInfo.id
    state.userInfo.name = userInfo.name
    state.userInfo.sex = userInfo.sex
    state.userInfo.password = userInfo.password
    state.userInfo.account = userInfo.account
    state.userInfo.img = userInfo.img
    state.userInfo.role = userInfo.role
    state.userInfo.state = userInfo.state
    state.userInfo.email = userInfo.email
    state.userInfo.phone = userInfo.phone
    state.userInfo.createTime = userInfo.createTime
    state.userInfo.updateTime = userInfo.updateTime
  },
  RESET_USERINFO: (state) => {
    state.userInfo.id = ''
    state.userInfo.name = ''
    state.userInfo.sex = ''
    state.userInfo.password = ''
    state.userInfo.account = ''
    state.userInfo.img = ''
    state.userInfo.role = ''
    state.userInfo.state = ''
    state.userInfo.email = ''
    state.userInfo.phone = ''
    state.userInfo.createTime = ''
    state.userInfo.updateTime = ''
  },
  SET_ID: (state, id) => {
    state.id = id
  },

  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_ACCOUNT: (state, account) => {
    state.account = account
  },
  // 新增数据：
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },
  SET_CREATE_TIME: (state, createTime) => {
    state.createTime = createTime
  },
  SET_UPDATE_TIME: (state, updateTime) => {
    state.updateTime = updateTime
  },
  // SET_AVATAR: (state, avatar) => {
  //   state.avatar = avatar
  // },
  SET_ROLE: (state, role) => {
    // state.role = {role: role}
    // state.role = role.push(role)
    state.role = role
  },
  SET_IMG: (state, img) => {
    state.img = img
  },
  SET_SEX: (state, sex) => {
    state.sex = sex
  },
  SET_STATES: (state, states) => {
    state.states = states
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    // userinfo是登录时传过来的表单对象，将里面的account和password单独拿出来
    const { account, password } = userInfo
    return new Promise((resolve, reject) => {
      // 发送网络请求进行登录操作
      login({ account: account.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        // commit('SET_USERINFO', data.user)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        // const { data } = response
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        commit('SET_USERINFO', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        // commit('SET_TOKEN', '')
        commit('RESET_USERINFO')
        removeToken()
        resetRouter()

        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, { root: true })

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 注销登录：清除token，remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      // commit('SET_TOKEN', '')
      commit('RESET_USERINFO')
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  async changeRoles({ commit, dispatch }, roles) {
    const token = roles + '-token'

    commit('SET_TOKEN', token)
    setToken(token)

    const { role } = await dispatch('getInfo')

    resetRouter()

    // generate accessible routes map based on roles
    const accessRoutes = await dispatch('permission/generateRoutes', role, { root: true })
    console.log('accessRoutes')
    console.log(accessRoutes)
    // dynamically add accessible routes
    router.addRoutes(accessRoutes)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, { root: true })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
